<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="optparse-cleanup.html" />
<link rel="prev" href="optparse-querying-manipulating-option-parser.html" />
<link rel="parent" href="optparse-reference-guide.html" />
<link rel="next" href="optparse-cleanup.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>14.3.3.9 Conflicts between options</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="14.3.3.8 querying and manipulating"
  href="optparse-querying-manipulating-option-parser.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="14.3.3 reference Guide"
  href="optparse-reference-guide.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="14.3.3.10 Cleanup"
  href="optparse-cleanup.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="optparse-querying-manipulating-option-parser.html">14.3.3.8 Querying and manipulating</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="optparse-reference-guide.html">14.3.3 Reference Guide</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="optparse-cleanup.html">14.3.3.10 Cleanup</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h3><a name="SECTION0016339000000000000000"></a><a name="optparse-conflicts-between-options"></a>
<br>
14.3.3.9 Conflicts between options
</h3>

<p>
If you're not careful, it's easy to define options with conflicting
option strings:
<div class="verbatim"><pre>
parser.add_option("-n", "--dry-run", ...)
[...]
parser.add_option("-n", "--noisy", ...)
</pre></div>

<p>
(This is particularly true if you've defined your own OptionParser
subclass with some standard options.)

<p>
Every time you add an option, <tt class="module">optparse</tt> checks for conflicts with existing
options.  If it finds any, it invokes the current conflict-handling
mechanism.  You can set the conflict-handling mechanism either in the
constructor:
<div class="verbatim"><pre>
parser = OptionParser(..., conflict_handler=handler)
</pre></div>

<p>
or with a separate call:
<div class="verbatim"><pre>
parser.set_conflict_handler(handler)
</pre></div>

<p>
The available conflict handlers are:
<blockquote>
</blockquote><dl>
<dt><strong><code>error</code> (default)</strong></dt>
<dd>assume option conflicts are a programming error and raise 
OptionConflictError
</dd>
<dt><strong><code>resolve</code></strong></dt>
<dd>resolve option conflicts intelligently (see below)
</dd>
</dl><blockquote>
</blockquote>

<p>
As an example, let's define an OptionParser that resolves conflicts
intelligently and add conflicting options to it:
<div class="verbatim"><pre>
parser = OptionParser(conflict_handler="resolve")
parser.add_option("-n", "--dry-run", ..., help="do no harm")
parser.add_option("-n", "--noisy", ..., help="be noisy")
</pre></div>

<p>
At this point, <tt class="module">optparse</tt> detects that a previously-added option is already
using the <code>"-n"</code> option string.  Since <code>conflict_handler</code> is
<code>"resolve"</code>, it resolves the situation by removing <code>"-n"</code> from the
earlier option's list of option strings.  Now <code>"-dry-run"</code> is the
only way for the user to activate that option.  If the user asks for
help, the help message will reflect that:
<div class="verbatim"><pre>
options:
  --dry-run     do no harm
  [...]
  -n, --noisy   be noisy
</pre></div>

<p>
It's possible to whittle away the option strings for a previously-added
option until there are none left, and the user has no way of invoking
that option from the command-line.  In that case, <tt class="module">optparse</tt> removes that
option completely, so it doesn't show up in help text or anywhere else.
Carrying on with our existing OptionParser:
<div class="verbatim"><pre>
parser.add_option("--dry-run", ..., help="new dry-run option")
</pre></div>

<p>
At this point, the original <b class="programopt">-n/-dry-run</b> option is no longer
accessible, so <tt class="module">optparse</tt> removes it, leaving this help text:
<div class="verbatim"><pre>
options:
  [...]
  -n, --noisy   be noisy
  --dry-run     new dry-run option
</pre></div>

<p>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="14.3.3.8 querying and manipulating"
  href="optparse-querying-manipulating-option-parser.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="14.3.3 reference Guide"
  href="optparse-reference-guide.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="14.3.3.10 Cleanup"
  href="optparse-cleanup.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="optparse-querying-manipulating-option-parser.html">14.3.3.8 Querying and manipulating</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="optparse-reference-guide.html">14.3.3 Reference Guide</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="optparse-cleanup.html">14.3.3.10 Cleanup</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
